package com.nlgc365.designpattern.Chapter04_Pattern.Behavior.P01_Observer.MiniEventBus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 作用: 储存要执行的类实例的方法
 */
public class ObserverAction {
    private static final Logger logger = LoggerFactory.getLogger(ObserverAction.class);
    // 属性1: 类实例
    private Object target;

    // 属性2: 方法
    private Method method;

    public ObserverAction(Object target, Method method) {
        this.target = target;
        this.method = method;
    }

    // 方法1: 执行
    public void execute(Object event) {
        try {
            method.invoke(target, event);
        } catch (InvocationTargetException | IllegalAccessException e) {
            logger.error("Method invoke error!", e);
            throw new RuntimeException(e);
        }
    }
}
